package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Goods;
import com.wms.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.lang.model.type.ReferenceType;
import java.util.HashMap;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam queryPageParam){
        HashMap param = queryPageParam.getParam();
        String name = (String) param.get("name");
        String goodstype = (String) param.get("goodstype");
        String storage = (String) param.get("storage");


        Page<Goods> page = new Page();
        page.setSize(queryPageParam.getPageSize());
        page.setCurrent(queryPageParam.getPageNum());
        LambdaQueryWrapper<Goods> query = new LambdaQueryWrapper<Goods>();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)){
            query.like(Goods::getName,name);
        }
        if (StringUtils.isNotBlank(goodstype) && !"null".equals(goodstype)){
            query.like(Goods::getGoodstype,goodstype);
        }
        if (StringUtils.isNotBlank(storage) && !"null".equals(storage)){
            query.like(Goods::getStorage,storage);
        }
        IPage iPage = goodsService.pageC(page, query);
        return Result.suc(iPage.getRecords(),iPage.getTotal());
    }


    @PostMapping("/update")
    public Result update(@RequestBody Goods goods){
        return goodsService.updateById(goods)?Result.suc():Result.fail();
    }

    @PostMapping("/save")
    public Result save(@RequestBody Goods goods){
        return goodsService.save(goods)?Result.suc():Result.fail();
    }

    @PostMapping("/del")
    public Result del(Integer id){
        return Result.suc(goodsService.removeById(id));
    }
}
